/**
 * Created by 胡婉飞 on 2016/9/9.
 */

$(function () {
    //注册
    $('#btn_regist').click(function () {
        location.href="/register";
    });
    $('#btn_start').click(function () {
        location.href="/register";
    });
    //登录
    $('#btn_login').click(function () {
        location.href="/login";
    });

    var page=1;
    $('#addart_form').submit(function () {
        var formdata=new FormData($('#addart_form')[0]);
        $.ajax({
            type: 'POST',
            url: '/addart',
            data:formdata,
            async:false,
            cache:false,
            contentType:false,
            processData:false,
            success: function (data) {
                if(data.res=="1"){
                    alert("上传成功")
                }else{
                    alert("请登录后再上传");
                    location.href='/login'
                }
            },

            error: function (data) {
                alert(data);
            }
        });
        return false;
    });

    $(window).scroll(function(){
        var scrollTop = $(this).scrollTop();
        var scrollHeight = $(document).height();
        var windowHeight = $(this).height();
        if(scrollTop + windowHeight == scrollHeight){
            $.ajax({
                type:'post',
                url:'/article',
                data:{page:page++},
                dataType:'json',
                success:function (data) {
                    if(data.res.length==0){
                        alert('没有更多内容了')
                    }else{

                        var img=data.res[0].pic;
                        var icon=data.res[0].icon;
                        var title= data.res[0].title;
                        var date=data.res[0].date;
                        var username=data.res[0].username;
                        var artno=data.res[0].artno;
                        var content=data.res[0].content;
                        $('.main').append(
                            '<div class="col-xs-4 col-sm-12 col-md-12 col-lg-3">'+
                            '<div class="author">'+
                            '<a href="#">'+
                            '<img src="../images/'+img+'" alt="" class="img_icon">'+
                            '&nbsp;'+username+
                            '</a>'+
                            '</div>'+
                            '<div class="articles_date">'
                            + date +
                            ' </div>'+
                            ' </div>'+
                            ' <div class="author_article">'+
                            '  <img src="../images/'+icon+'" alt="" class="img_01" style="float: left">'+
                            '      <h4 style="color: #dfc369;font-size: 1.8rem" class="simple"><a href="#">'+title+'</a></h4>'+
                            '<p class="simple">'+content+'</p>'+
                                '<br>'+
                                '<br>'+
                            '<p>&nbsp;<a href="article/'+artno+'">阅读全文</a></p>'+
                            '</div>'+
                            '<hr class="hr">'
                        );

                    }
                },
                error:function (error) {
                    alert(error.message)

                }

            });

        }
    });

    $('.picture').click(function(){
        $(this).css("background-color","#FF0000")
    });

    function preview1(file) {
        var img = new Image(), url = img.src = URL.createObjectURL(file);
        var $img = $(img);
        img.onload = function() {
            URL.revokeObjectURL(url);
            $('#preview').empty().append($img)
        }
    }
    function preview2(file) {
        var reader = new FileReader();
        reader.onload = function(e) {
            var $img = $('<img>').attr("src", e.target.result);
            $('#preview').empty().append($img)
        };
        reader.readAsDataURL(file)
    }

    $(function() {
        $('[type=file]').change(function(e) {
            var file = e.target.files[0];
            preview1(file)
        });

    })


});

